#include <linux/init.h>
#include <linux/module.h>
#include <linux/err.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/kernel.h>
#include <linux/kobject.h>


// 定义2个kobject指针变量
struct kobject *mykobject01;
struct kobject *mykobject02;
struct kobject *mykobject03;

struct kset *mykset;

// //定义了一个kobj_type结构体变量, 用于描述kobject类型
struct kobj_type mykobj_type;



static int __init mykobject_init(void)
{   
    int ret;
    printk(KERN_EMERG "mykobject_init OK!\n");
    //  创建并添加 kset
    mykset = kset_create_and_add("mykset", NULL, NULL);

    // 创建kobject并设定kset
    mykobject01 = kzalloc(sizeof(struct kobject), GFP_KERNEL);
    mykobject01->kset = mykset;
    ret = kobject_init_and_add(mykobject01, &mykobj_type, NULL, "%s", "mykobject01");
    printk(KERN_EMERG "mykobject01 kref is %d\n", kref_read(&mykobject01->kref));
    printk(KERN_EMERG "----------------------\n");


    // 创建kobject并设定kset
    mykobject02 = kzalloc(sizeof(struct kobject), GFP_KERNEL);
    mykobject02->kset = mykset;
    ret = kobject_init_and_add(mykobject02, &mykobj_type, NULL, "%s", "mykobject02");
    printk(KERN_EMERG "mykobject01 kref is %d\n", kref_read(&mykobject01->kref));
    printk(KERN_EMERG "mykobject02 kref is %d\n", kref_read(&mykobject02->kref));
    printk(KERN_EMERG "----------------------\n");

    mykobject03 =  kobject_create_and_add("mykboject03", mykobject01);
    printk(KERN_EMERG "mykobject01 kref is %d\n", kref_read(&mykobject01->kref));
    printk(KERN_EMERG "mykobject02 kref is %d\n", kref_read(&mykobject02->kref));
    printk(KERN_EMERG "mykobject03 kref is %d\n", kref_read(&mykobject03->kref));
    printk(KERN_EMERG "----------------------\n");


    return 0;
}



static void __exit mykojbect_exit(void)
{
    printk(KERN_EMERG "mykobject01 kref is %d\n", kref_read(&mykobject01->kref));
    printk(KERN_EMERG "mykobject02 kref is %d\n", kref_read(&mykobject02->kref));
    printk(KERN_EMERG "mykobject03 kref is %d\n", kref_read(&mykobject03->kref));
    printk(KERN_EMERG "----------------------\n");
    // 释放之前创建的kobject对象
    kobject_put(mykobject03);
    printk(KERN_EMERG "mykobject01 kref is %d\n", kref_read(&mykobject01->kref));
    printk(KERN_EMERG "mykobject02 kref is %d\n", kref_read(&mykobject02->kref));
    printk(KERN_EMERG "mykobject03 kref is %d\n", kref_read(&mykobject03->kref));
    printk(KERN_EMERG "----------------------\n");
    // 释放之前创建的kobject对象
    kobject_put(mykobject02);
    printk(KERN_EMERG "mykobject01 kref is %d\n", kref_read(&mykobject01->kref));
    printk(KERN_EMERG "mykobject02 kref is %d\n", kref_read(&mykobject02->kref));
    printk(KERN_EMERG "mykobject03 kref is %d\n", kref_read(&mykobject03->kref));
    printk(KERN_EMERG "----------------------\n");

    kobject_put(mykobject01);
    printk(KERN_EMERG "mykobject01 kref is %d\n", kref_read(&mykobject01->kref));
    printk(KERN_EMERG "mykobject03 kref is %d\n", kref_read(&mykobject03->kref));
    printk(KERN_EMERG "mykobject02 kref is %d\n", kref_read(&mykobject02->kref));
    printk(KERN_EMERG "----------------------\n");
    // 释放kset
    kset_put(mykset);
    printk(KERN_EMERG "mykojbect_exit OK!\n");  
}



module_init(mykobject_init);
module_exit(mykojbect_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("Treasure1029");
